//
//  TSWebAddToDiffViewController.m
//  AppCommonJSCollect
//
//  Created by qian on 2023/10/20.
//  Copyright © 2023 dvlproad. All rights reserved.
//

#import "TSWebAddToDiffViewController.h"
#import <Masonry/Masonry.h>
#import "TSSharedNormalViewContainer.h"
#import "TSSharedWebViewContainer.h"

#import "TSMineHomeViewController.h"

@interface TSWebAddToDiffViewController () {
    
}
@property (nonatomic, copy) NSString *Url;
@property (nonatomic, strong) TSSharedWebViewContainer *sharedView;
@property (nonatomic, assign) NSInteger currentPageIndex;

@end

@implementation TSWebAddToDiffViewController

- (NSString *)urlWithUrlParams:(nullable NSDictionary *)params {
//    NSString *homeUrl = [[NSBundle mainBundle] pathForResource:@"dvlp_mini_program_demo_JavascriptBridge.html" ofType:nil];
//    NSString *homeUrl = @"https://baidu.com";
    NSString *homeUrl = @"http://localhost/h5_native_interacte/h5_mini_program/dvlp_mini_program_demo_JavascriptBridge.html?title=test&randomNum=000";
//    NSString *homeUrl = @"https://dvlproad.github.io/Architecture%E6%9E%B6%E6%9E%84/h5_native_interacte/h5js/dvlp_h5js_demo.html";
//    NSLog(@"homeUrl = %@", homeUrl);
    if ([homeUrl hasPrefix:@"http"] == NO || params == nil) {
        return homeUrl;
    }
    
    // 创建NSURLComponents对象，并设置基础URL
    NSURLComponents *urlComponents = [NSURLComponents componentsWithString:homeUrl];
    // 创建NSURLQueryItem对象，并设置参数
    NSMutableArray<NSURLQueryItem *> *queryItems = [NSMutableArray array];
    for (NSString *key in params) {
        NSString *value = params[key];
        NSURLQueryItem *queryItem = [NSURLQueryItem queryItemWithName:key value:value];
        [queryItems addObject:queryItem];
    }
    urlComponents.queryItems = queryItems;  // 设置参数到NSURLComponents对象中

    // 获取拼接后的完整URL路径
    NSString *fullUrlString = urlComponents.URL.absoluteString;
    NSLog(@"完整路径: %@", fullUrlString);
    
    return fullUrlString;
}

- (instancetype)initWithUrlParams:(nullable NSDictionary *)params  {
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        self.Url = [self urlWithUrlParams:params];
    }
    return self;
}



- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
    
    [self.sharedView recoverOldTranslucent:self.navigationController.navigationBar];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    [self.sharedView getCurrentTranslucent:self.navigationController.navigationBar];
    
    [self showSharedView];
    
    [self.sharedView reloadUrl:self.Url];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0]; // #f5f5f5
    [self setupViews];
}


- (void)showSharedView {
    [self.view addSubview:self.sharedView];
    [self.sharedView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view).mas_offset(0);
        make.top.mas_equalTo(self.view).mas_offset(0);
        make.bottom.mas_equalTo(self.view).mas_offset(0);
    }];
    /*
    if ([self.view.subviews containsObject:self.sharedView]) {
        //[self.view addSubview:self.sharedView];
        [self.sharedView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.view);
            make.left.mas_equalTo(self.view).mas_offset(0);
            make.top.mas_equalTo(self.view).mas_offset(0);
            make.bottom.mas_equalTo(self.view).mas_offset(0);
        }];
    } else {
        [self.view addSubview:self.sharedView];
        [self.sharedView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.view);
            make.left.mas_equalTo(self.view).mas_offset(0);
            make.top.mas_equalTo(self.view).mas_offset(0);
            make.bottom.mas_equalTo(self.view).mas_offset(0);
        }];
    }
    */
}

- (void)closeMp {
    [self.navigationController popToRootViewControllerAnimated:YES];
    NSLog(@"self.navigationController.viewControllers = %@", self.navigationController.viewControllers);
}


- (void)goBackToApp {
    [self.navigationController popViewControllerAnimated:YES];
    NSLog(@"self.navigationController.viewControllers = %@", self.navigationController.viewControllers);
}

- (void)goAppPage:(NSString *)userId {
    TSMineHomeViewController *viewController = [[TSMineHomeViewController alloc] initWithUserId:userId];
    if (self.navigationController == nil) {
        NSLog(@"self.navigationController = %@", self.navigationController);
        return;
    }
    [self.navigationController pushViewController:viewController animated:YES];
}


- (void)setupViews {
    //[self showSharedView];
}


#pragma mark ---------  懒加载  --------------
//- (TSSharedNormalViewContainer *)sharedView {
//    if (!_sharedView) {
//        _sharedView = [TSSharedNormalViewContainer sharedInstance];
//    }
//    return _sharedView;
//}
- (TSSharedWebViewContainer *)sharedView {
    if (!_sharedView) {
        TSSharedWebViewContainer *sharedView = [TSSharedWebViewContainer sharedInstance];
        [sharedView setupJSHandle_closeMiniProgram:^{
            __weak typeof(self)weakSelf = self;
            [weakSelf closeMp];
        } hideMiniProgram:^{
            __weak typeof(self)weakSelf = self;
            [weakSelf goBackToApp];
        } openAppUserDetail1Handel:^(NSString * _Nonnull userId) {
            __weak typeof(self)weakSelf = self;
            [weakSelf goAppPage:userId];
        } openAppUserDetail2Handel:^(NSString * _Nonnull userId) {
            __weak typeof(self)weakSelf = self;
            [weakSelf goAppPage:userId];
        }];
        _sharedView = sharedView;
    }
    return _sharedView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
